package com.controller;

import java.io.File;
import java.io.IOException;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.entity.Liuyanxinxi;
import com.server.LiuyanxinxiServer;
import com.util.PageBean;
import net.sf.json.JSONObject;
import com.util.db;
import java.sql.SQLException;
import java.sql.*;
@Controller
public class LiuyanxinxiController {
	@Resource
	private LiuyanxinxiServer liuyanxinxiService;

	@RequestMapping("addLiuyanxinxi.do")
	public String addLiuyanxinxi(HttpServletRequest request,Liuyanxinxi liuyanxinxi,HttpSession session) throws SQLException{
		Timestamp time=new Timestamp(System.currentTimeMillis());
		
		liuyanxinxi.setAddtime(time.toString().substring(0, 19));
		liuyanxinxiService.add(liuyanxinxi);
		db dbo = new db();
		
		//kuabiaogaizhi
		session.setAttribute("backxx", "添加成功");session.setAttribute("backurl", request.getHeader("Referer"));return "redirect:postback.jsp";
		
	}
	@RequestMapping("addLiuyanxinxiqt.do")
	public String addLiuyanxinxiqt(HttpServletRequest request,Liuyanxinxi liuyanxinxi,HttpSession session) throws SQLException{
		Timestamp time=new Timestamp(System.currentTimeMillis());
		
		liuyanxinxi.setAddtime(time.toString().substring(0, 19));
		liuyanxinxiService.add(liuyanxinxi);
		db dbo = new db();
		
		//kuabiaogaizhi
		session.setAttribute("backxx", "添加成功");
		session.setAttribute("backurl", request.getHeader("Referer"));
		return "redirect:postback.jsp";
	}
 
//	处理编辑
	@RequestMapping("doUpdateLiuyanxinxi.do")
	public String doUpdateLiuyanxinxi(int id,ModelMap map,Liuyanxinxi liuyanxinxi){
		liuyanxinxi=liuyanxinxiService.getById(id);
		map.put("liuyanxinxi", liuyanxinxi);
		return "liuyanxinxi_updt";
	}
	
	
	@RequestMapping("doUpdateLiuyanxinxilb.do")
	public String doUpdateLiuyanxinxilb(int id,ModelMap map,Liuyanxinxi liuyanxinxi){
		liuyanxinxi=liuyanxinxiService.getById(id);
		map.put("liuyanxinxi", liuyanxinxi);
		return "liuyanxinxi_updtlb";
	}
	
@RequestMapping("updateLiuyanxinxilb.do")
	public String updateLiuyanxinxilb(int id,ModelMap map,Liuyanxinxi liuyanxinxi){
		liuyanxinxiService.updatelb(liuyanxinxi);
		return "redirect:liuyanxinxiList.do";
	}
	
//	后台详细
	@RequestMapping("liuyanxinxiDetail.do")
	public String liuyanxinxiDetail(int id,ModelMap map,Liuyanxinxi liuyanxinxi){
		liuyanxinxi=liuyanxinxiService.getById(id);
		map.put("liuyanxinxi", liuyanxinxi);
		return "liuyanxinxi_detail";
	}
//	前台详细
	@RequestMapping("lyxxDetail.do")
	public String lyxxDetail(int id,ModelMap map,Liuyanxinxi liuyanxinxi){
		liuyanxinxi=liuyanxinxiService.getById(id);
		map.put("liuyanxinxi", liuyanxinxi);
		return "liuyanxinxidetail";
	}
//	
	@RequestMapping("updateLiuyanxinxi.do")
	public String updateLiuyanxinxi(int id,ModelMap map,Liuyanxinxi liuyanxinxi,HttpServletRequest request,HttpSession session){
		liuyanxinxiService.update(liuyanxinxi);
		
		session.setAttribute("backxx", "操作成功");session.setAttribute("backurl", request.getHeader("Referer"));return "redirect:postback.jsp";
	}

//	分页查询
	@RequestMapping("liuyanxinxiList.do")
	public String liuyanxinxiList(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Liuyanxinxi liuyanxinxi, String biaoti, String yonghuming, String xingming, String shouji, String liuyanneirong, String huifu){
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 8);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 8);
		
		
		if(biaoti==null||biaoti.equals("")){pmap.put("biaoti", null);}else{pmap.put("biaoti", biaoti);}
		if(yonghuming==null||yonghuming.equals("")){pmap.put("yonghuming", null);}else{pmap.put("yonghuming", yonghuming);}
		if(xingming==null||xingming.equals("")){pmap.put("xingming", null);}else{pmap.put("xingming", xingming);}
		if(shouji==null||shouji.equals("")){pmap.put("shouji", null);}else{pmap.put("shouji", shouji);}
		if(liuyanneirong==null||liuyanneirong.equals("")){pmap.put("liuyanneirong", null);}else{pmap.put("liuyanneirong", liuyanneirong);}
		if(huifu==null||huifu.equals("")){pmap.put("huifu", null);}else{pmap.put("huifu", huifu);}
		
		int total=liuyanxinxiService.getCount(pmap);
		pageBean.setTotal(total);
		List<Liuyanxinxi> list=liuyanxinxiService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "liuyanxinxi_list";
	}
	@RequestMapping("liuyanxinxiList2.do")
	public String liuyanxinxiList2(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Liuyanxinxi liuyanxinxi, String biaoti, String yonghuming, String xingming, String shouji, String liuyanneirong, String huifu,HttpServletRequest request){
		/*if(session.getAttribute("user")==null){
			return "login";
		}*/
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 15);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 15);
		
		pmap.put("yonghuming", (String)request.getSession().getAttribute("username"));
		if(biaoti==null||biaoti.equals("")){pmap.put("biaoti", null);}else{pmap.put("biaoti", biaoti);}
		if(xingming==null||xingming.equals("")){pmap.put("xingming", null);}else{pmap.put("xingming", xingming);}
		if(shouji==null||shouji.equals("")){pmap.put("shouji", null);}else{pmap.put("shouji", shouji);}
		if(liuyanneirong==null||liuyanneirong.equals("")){pmap.put("liuyanneirong", null);}else{pmap.put("liuyanneirong", liuyanneirong);}
		if(huifu==null||huifu.equals("")){pmap.put("huifu", null);}else{pmap.put("huifu", huifu);}
		
		
		int total=liuyanxinxiService.getCount(pmap);
		pageBean.setTotal(total);
		List<Liuyanxinxi> list=liuyanxinxiService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "liuyanxinxi_list2";
	}
	
	
	@RequestMapping("lyxxList.do")
	public String lyxxList(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Liuyanxinxi liuyanxinxi, String biaoti, String yonghuming, String xingming, String shouji, String liuyanneirong, String huifu){
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 8);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 8);
		if(biaoti==null||biaoti.equals("")){pmap.put("biaoti", null);}else{pmap.put("biaoti", biaoti);}
		if(yonghuming==null||yonghuming.equals("")){pmap.put("yonghuming", null);}else{pmap.put("yonghuming", yonghuming);}
		if(xingming==null||xingming.equals("")){pmap.put("xingming", null);}else{pmap.put("xingming", xingming);}
		if(shouji==null||shouji.equals("")){pmap.put("shouji", null);}else{pmap.put("shouji", shouji);}
		if(liuyanneirong==null||liuyanneirong.equals("")){pmap.put("liuyanneirong", null);}else{pmap.put("liuyanneirong", liuyanneirong);}
		if(huifu==null||huifu.equals("")){pmap.put("huifu", null);}else{pmap.put("huifu", huifu);}
		
		int total=liuyanxinxiService.getCount(pmap);
		pageBean.setTotal(total);
		List<Liuyanxinxi> list=liuyanxinxiService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "liuyanxinxilist";
	}
	@RequestMapping("lyxxListtp.do")
	public String lyxxListtp(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Liuyanxinxi liuyanxinxi, String biaoti, String yonghuming, String xingming, String shouji, String liuyanneirong, String huifu){
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 8);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 8);
		if(biaoti==null||biaoti.equals("")){pmap.put("biaoti", null);}else{pmap.put("biaoti", biaoti);}
		if(yonghuming==null||yonghuming.equals("")){pmap.put("yonghuming", null);}else{pmap.put("yonghuming", yonghuming);}
		if(xingming==null||xingming.equals("")){pmap.put("xingming", null);}else{pmap.put("xingming", xingming);}
		if(shouji==null||shouji.equals("")){pmap.put("shouji", null);}else{pmap.put("shouji", shouji);}
		if(liuyanneirong==null||liuyanneirong.equals("")){pmap.put("liuyanneirong", null);}else{pmap.put("liuyanneirong", liuyanneirong);}
		if(huifu==null||huifu.equals("")){pmap.put("huifu", null);}else{pmap.put("huifu", huifu);}
		
		int total=liuyanxinxiService.getCount(pmap);
		pageBean.setTotal(total);
		List<Liuyanxinxi> list=liuyanxinxiService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "liuyanxinxilisttp";
	}
	
	@RequestMapping("deleteLiuyanxinxi.do")
	public String deleteLiuyanxinxi(int id,HttpServletRequest request,HttpSession session){
		liuyanxinxiService.delete(id);
		session.setAttribute("backxx", "删除成功");session.setAttribute("backurl", request.getHeader("Referer"));return "redirect:postback.jsp";
	}
	
	
}
